package com.renwei.yunlong.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.utils.ToastUtil;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.zyf.fwms.commonlibrary.utils.AutoUtils;
import com.zyf.fwms.commonlibrary.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.renwei.yunlong.R.id.cancel_action;
import static com.renwei.yunlong.R.id.rl_wechat;

/**
 * 分享--页面
 * <p>
 * Created by Administrator on 2017/8/25.
 */

public class ShareActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.btn_share)
    Button btnShare;

    private PopupWindow popupWindow;

    Button btnCancel;
    RelativeLayout rlWechat,rlWechatFriends,rlQQFriends,rlQQSpace,rlSinaWeibo;
    View view;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_share);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        //屏幕自适配
        AutoUtils.setSize(this, false, 720, 1280);
        AutoUtils.auto(this);

        initData();
    }

    private void initData() {
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(this);
        tvTitle.setText("分享");
        btnShare.setOnClickListener(this);

        view = LayoutInflater.from(this).inflate(R.layout.share_view, null);
        rlWechat = (RelativeLayout) view.findViewById(R.id.rl_wechat);
        rlWechatFriends = (RelativeLayout) view.findViewById(R.id.rl_wechat_friends);
        rlQQFriends = (RelativeLayout) view.findViewById(R.id.rl_qq_friends);
        rlQQSpace = (RelativeLayout) view.findViewById(R.id.rl_qq_space);
        rlSinaWeibo = (RelativeLayout) view.findViewById(R.id.rl_sina_weibo);
        btnCancel = (Button) view.findViewById(R.id.btn_cancel);

        rlWechat.setOnClickListener(this);
        rlWechatFriends.setOnClickListener(this);
        rlQQFriends.setOnClickListener(this);
        rlQQSpace.setOnClickListener(this);
        rlSinaWeibo.setOnClickListener(this);
        btnCancel.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_share://分享
                showPopuWindow(view);
                break;
            case R.id.rl_wechat:
                UMWeb web = new UMWeb("http://www.baidu.com");
                web.setTitle("百度");//标题
                web.setThumb(new UMImage(ShareActivity.this, R.mipmap.ic_launcher));  //缩略图
                web.setDescription("邀请您注册云呐");//描述
                new ShareAction(ShareActivity.this)
                        .setPlatform(SHARE_MEDIA.WEIXIN)//传入平台
                        .withMedia(web)
                        .setCallback(shareListener)//回调监听器
                        .share();
                break;
            case R.id.rl_wechat_friends:
                web = new UMWeb("http://www.baidu.com");
                web.setTitle("百度");//标题
                web.setThumb(new UMImage(ShareActivity.this, R.mipmap.ic_launcher));  //缩略图
                web.setDescription("邀请您注册云呐");//描述

                new ShareAction(ShareActivity.this)
                        .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)//传入平台
                        .withMedia(web)
                        .setCallback(shareListener)//回调监听器
                        .share();
                break;
            case R.id.rl_qq_friends:
                web = new UMWeb("http://www.baidu.com");
                web.setTitle("百度");//标题
                web.setThumb(new UMImage(ShareActivity.this, R.mipmap.ic_launcher));  //缩略图
                web.setDescription("邀请您注册云呐");//描述

                new ShareAction(ShareActivity.this)
                        .setPlatform(SHARE_MEDIA.QQ)//传入平台
                        .withMedia(web)
                        .setCallback(shareListener)//回调监听器
                        .share();
                break;
            case R.id.rl_qq_space:
                web = new UMWeb("http://www.baidu.com");
                web.setTitle("百度");//标题
                web.setThumb(new UMImage(ShareActivity.this, R.mipmap.ic_launcher));  //缩略图
                web.setDescription("邀请您注册云呐");//描述

                new ShareAction(ShareActivity.this)
                        .setPlatform(SHARE_MEDIA.QZONE)//传入平台
                        .withMedia(web)
                        .setCallback(shareListener)//回调监听器
                        .share();
                break;
            case R.id.rl_sina_weibo:
                web = new UMWeb("http://www.baidu.com");
                web.setTitle("百度");//标题
                web.setThumb(new UMImage(ShareActivity.this, R.mipmap.ic_launcher));  //缩略图
                web.setDescription("邀请您注册云呐");//描述

                new ShareAction(ShareActivity.this)
                        .setPlatform(SHARE_MEDIA.SINA)//传入平台
                        .withMedia(web)
                        .setCallback(shareListener)//回调监听器
                        .share();
                break;
            case R.id.btn_cancel:
                popupWindow.dismiss();
                break;
            default:
                break;
        }
    }

    //popupWindow弹框
    private void showPopuWindow(View view){
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        popupWindow.showAtLocation(view,Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });
        backgroundAlpha(this,0.5f);//0.0-1.0
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // TODO Auto-generated method stub
                backgroundAlpha(ShareActivity.this,1f);
            }
        });
        ColorDrawable dw = new ColorDrawable(0xb0808080);
        popupWindow.setBackgroundDrawable(dw);
        popupWindow.showAsDropDown(view,0,0);
        // 设置好参数之后再show
        popupWindow.showAsDropDown(view);
    }
    /**
     * 设置添加屏幕的背景透明度
     * @param bgAlpha
     */
    public void backgroundAlpha(Activity context, float bgAlpha)
    {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    private UMShareListener shareListener = new UMShareListener() {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {
        }
        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
            ToastUtil.showShort(ShareActivity.this,"分享成功");
        }
        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            ToastUtil.showShort(ShareActivity.this,"分享失败");
        }
        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
            ToastUtil.showShort(ShareActivity.this,"取消");
        }
    };
}
